package org.example.controller.borrow;


import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.config.MyPrinciple;
import org.example.domain.Book;
import org.example.dto.Id;
import org.example.dto.Isbn;
import org.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.List;

/**
 * @author tangtl
 * @create 2021-8-9 14:14
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Autowired
    BookService bookService;


    @PostMapping("/get-token-info")
    @ApiOperation("用token获得用户信息")
    public Boolean getWxGetToken(@ApiIgnore HttpSession session, @ApiIgnore MyPrinciple principal,@RequestBody Isbn req) {
        log.info("session:{}",session.getId());
        log.info("principal:{}",principal);
        return bookService.preBoroow(principal.getUserId(),req.getIsbn());
    }

    @PostMapping("/pre-borrow")
    @ApiOperation("预借")
    public boolean preBorrow(@ApiIgnore HttpSession session, @ApiIgnore MyPrinciple principal, @RequestBody Isbn isbn)  {
        log.info("session:{}",session.getId());
        log.info("principal:{}",principal);

        return bookService.preBoroow(principal.getUserId(),isbn.getIsbn());
    }

    @PostMapping("/borrow")
    @ApiOperation("预借")
    public boolean borrow(@ApiIgnore HttpSession session, @ApiIgnore MyPrinciple principal, @RequestBody Id isbn)  {
        log.info("session:{}",session.getId());
        log.info("principal:{}",principal);

        return bookService.boroow(principal.getUserId(),isbn.getId());
    }

    @PostMapping("/pre-book-return")
    @ApiOperation("预借")
    public boolean preBookReturn(@ApiIgnore HttpSession session, @ApiIgnore MyPrinciple principal, @RequestBody Id isbn)  {
        log.info("session:{}",session.getId());
        log.info("principal:{}",principal);

        return bookService.preBookReturn(principal.getUserId(),isbn.getId());
    }

    @PostMapping("/book-return")
    @ApiOperation("预借")
    public boolean bookReturn(@ApiIgnore HttpSession session, @ApiIgnore MyPrinciple principal, @RequestBody Id isbn)  {
        log.info("session:{}",session.getId());
        log.info("principal:{}",principal);

        return bookService.bookReturn(principal.getUserId(),isbn.getId());
    }

    @GetMapping("/search")
    @ApiOperation("所有图书列表")
    public List<Book> getBhList(@RequestParam String searchStr)  {
        return bookService.searchBook(searchStr);
    }

    @GetMapping("/{isbn}")
    @ApiOperation("所有图书列表")
    public Book getBook(@ApiIgnore HttpSession session, @ApiIgnore MyPrinciple principal,@PathVariable String isbn){
        log.info("pri>>>>>>>");
        log.info("session:{}",session.getId());
        log.info("principal:{}",principal);
        return bookService.getBook(isbn);
    }

}
